package com.mawc.equipower.utils;

import com.mawc.equipower.model.PaserMode;
import com.mawc.equipower.model.ShowModel;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CalculateData
 * @ProjectName equippower
 * @Description
 * @Author 马文才
 * @Date 2021.10.31 18:22
 */
public class CalculateData {

    public static List<ShowModel> showModels(List<PaserMode> paserModes){
        ArrayList<ShowModel> showModels = new ArrayList<>();
        for (PaserMode paserMode : paserModes) {
            ShowModel showModel = new ShowModel();
            String cal = paserMode.getGs();
            String content = paserMode.getContent();
            Integer integer = HexUtil.hexToInt(content);
            //这个是解析公式的
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("js");
            engine.put("a",integer);
            try {
                Object eval = engine.eval(cal);
                showModel.setVal(eval+"");
            } catch (ScriptException e) {
                e.printStackTrace();
            }
            showModel.setType(paserMode.getType());
            showModel.setTitle(paserMode.getIntro());
            showModels.add(showModel);
        }
        return showModels;
    }

}
